package com.cci.kangdao.task;

import com.cci.kangdao.service.DBUMessageService;
import com.cci.kangdao.service.NoticeService;
import com.cci.kangdao.service.RemindTaskService;
import com.cci.kangdao.utilTool.LocalTimeUtil;
import com.cci.kangdao.utilTool.NumberUtils;
import com.cci.kangdao.utilTool.SmsTool;
import com.cci.kangdao.utilTool.SmsUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author William Du
 * @date 2019/8/28
 */
public abstract class BaseTask {

    @Resource
    public RemindTaskService remindTaskService;
    @Resource
    private NoticeService noticeService;
    @Resource
    private DBUMessageService dbuMessageService;

    
    /**
     * 此方法不可以直接被修改，因为存在多个方法引用此方法
     */
    public void sendMsg(String phone, String orderNo, String userName, Long orderId, String userId, int function, int category) {
        List<Map<String, Object>> notifyRecordTMapList = new ArrayList<>();

        String smsText = "";
        switch (function) {
            case 14:
                smsText = "24小时服务超时，" + dbuMessageService.getUpgradeMessage(orderId);
                break;
            case 15:
                smsText = "48小时服务超时，" + dbuMessageService.getUpgradeMessage(orderId);
                break;
            case 16:
                smsText = "8小时服务超时，" + dbuMessageService.getUpgradeMessage(orderId);
                break;
            case 21:
                smsText = "4小时服务超时，" + dbuMessageService.getUpgradeMessage(orderId);
                break;
            default:
                smsText = "您有未处理工单，工单号" + orderNo + "，工单已超时，请尽快处理。";
                break;
        }

        //短信发送
        if (StringUtils.isNotEmpty(phone)) {
            // 发送短信操作
            SmsUtils.sendMessage(phone, smsText);
            // 拼装入库记录信息
            Map<String, Object> map = getMsgMap(orderId, smsText, userId, 1, function, category, 1);
            notifyRecordTMapList.add(map);
        }

        // 将发送记录列表存入数据库
        if (CollectionUtils.isNotEmpty(notifyRecordTMapList)) {
            remindTaskService.saveMsgOperation(notifyRecordTMapList);
        }
        // 发送推送
        if(null != userId && NumberUtils.isNumber(userId)){
            noticeService.push(function, category, orderId, Long.valueOf(userId), userName, null, smsText, "您有未处理工单", 0L);
        }else{
            noticeService.push(function, category, orderId, null, userName, null, smsText, "您有未处理工单", 0L);
        }
    }
    

    public Map<String, Object> getMsgMap(Long orderId, String smsText, String userId, int SendType, int function, int category, int status) {
        return getMsgMap(orderId, smsText, userId, SendType, function, category, status, "");
    }

    public Map<String, Object> getMsgMap(Long orderId, String smsText, String userId, int SendType, int function, int category, int status, String exceptionInfo) {
        Map<String, Object> notifyRecordTMap = new HashMap<>();
        //获取当前时间
        String currentTime = LocalTimeUtil.getRealCurrentTime();
        notifyRecordTMap.put("OrderId", orderId);
        notifyRecordTMap.put("Function", function);
        notifyRecordTMap.put("Category", category);
        //接收人
        notifyRecordTMap.put("ReceiverId", StringUtils.isEmpty(userId) ? null : Long.parseLong(userId));
        // 消息内容
        notifyRecordTMap.put("Content", smsText);
        //推送方式1代表短信  2代表app推送
        notifyRecordTMap.put("SendType", SendType);
        notifyRecordTMap.put("SendTime", currentTime);
        notifyRecordTMap.put("Status", status);
        notifyRecordTMap.put("ExceptionInfo", StringUtils.isEmpty(exceptionInfo) ? "" : exceptionInfo);
        notifyRecordTMap.put("IsRead", 0);
        notifyRecordTMap.put("Flag", 0);
        notifyRecordTMap.put("Creator", "");
        notifyRecordTMap.put("CreateTime", currentTime);
        return notifyRecordTMap;
    }

    
    /**
     * 服务工单未完成超时需求变更后新增加的方法
     */
    public void sendMsgForUnfinishedWorkOrder(String phone, String orderNo, String userName, Long orderId, String userId, int function, int category,Map<String,Object> overTimeOrder) {
        List<Map<String, Object>> notifyRecordTMapList = new ArrayList<>();

        String smsText = "";
        switch (function) {
            case 24:
	        	// 72小时超时
	    		smsText = dbuMessageService.getUpgradeMessageForUnfinishedWorkOrder(72, overTimeOrder);
	    		break;
        	case 15:
        		// 48小时超时
        		smsText = dbuMessageService.getUpgradeMessageForUnfinishedWorkOrder(48, overTimeOrder);
        		break;
            case 14:
            	// 24小时超时
                smsText = dbuMessageService.getUpgradeMessageForUnfinishedWorkOrder(24, overTimeOrder);
                break;
            case 23:
            	// 12小时超时
	    		smsText = dbuMessageService.getUpgradeMessageForUnfinishedWorkOrder(12, overTimeOrder);
	    		break;
            case 22:
            	// 6小时超时
	    		smsText = dbuMessageService.getUpgradeMessageForUnfinishedWorkOrder(6, overTimeOrder);
	    		break;
            case 21:
            	// 4小时超时
                smsText = dbuMessageService.getUpgradeMessageForUnfinishedWorkOrder(4, overTimeOrder);
                break;
            default:
                smsText = "您有未处理工单，工单号" + orderNo + "，工单已超时，请尽快处理。";
                break;
        }

        //短信发送
        if (StringUtils.isNotEmpty(phone)) {
            // 发送短信操作
            String sendResult = SmsUtils.sendMessage(phone, smsText);
            // 拼装入库记录信息
            Map<String, Object> map = getMsgMap(orderId, smsText, userId, 1, function, category, 1);
            
            // "00"状态码表示短信发送成功
            if(!"00".equals(sendResult)) {
            	map.put("Status", 0);
            	map.put("ExceptionInfo", String.format("短信发送失败 手机号:%s 状态码:%s", phone, sendResult));
            }else {
            	map.put("ExceptionInfo", String.format("短信发送成功 手机号:%s", phone));
            }
            
            notifyRecordTMapList.add(map);
        }

        // 将发送记录列表存入数据库
        if (CollectionUtils.isNotEmpty(notifyRecordTMapList)) {
            remindTaskService.saveMsgOperation(notifyRecordTMapList);
        }
        // 发送推送
        if(null != userId && NumberUtils.isNumber(userId)){
            noticeService.push(function, category, orderId, Long.valueOf(userId), userName, null, smsText, "您有未处理工单", 0L);
        }else{
            noticeService.push(function, category, orderId, null, userName, null, smsText, "您有未处理工单", 0L);
        }
    }
    
    
    
}
